package gui;
import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JSeparator;
import Entity.*;
import controllers.ManagerUIP;
import controllers.OBLSettings;
import controllers.WelcomeUIP;
import messagesPacks.*;

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.util.Date;
import java.util.Enumeration;
import messagesPacks.*;
import javax.swing.JList;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
 * @author Ariel Brucker
 * The UI CLASS FOR SUSPEND SUBSCRIBER(GUI)
 */
public class ManagerSuspendSub extends JPanel
{
	private static final long serialVersionUID = 1L;
	ManagerMainUI managerMainUI=null;
	private JLabel lblOblManager;
	public JButton btnLogout;
	public JButton btnExit;
	private JButton btnBack;
	private JScrollPane scrollPane;
	private JTextField searchBySubFName;
	private JTextField searchBySubLName;
	private JTextField searchBySubID;
	private JLabel lblByFNameLabel;
	private JLabel lblByLNameLabel;
	private JLabel lblBySIDLabel;
	public SimpleTable SubTable;
	private final JSeparator separator = new JSeparator();
	ManagerUIP muip=null; 
	private JButton btnSearch;
	private JButton btnClear;
	JRadioButton rdbtnSearch;
	JRadioButton rdbtnShowAll;
	JLabel lblSearchForUnsuspended;
	JLabel lblShowAllUnsuspended;
	JButton btnSuspend; 
	JRadioButton rdbtnShowLateSubscribers;
	JLabel lblPresentLateSubscribers;
	private int status_user;
	private int permission_user;
	private String id_user;
	private int rowToDelete;
	private JLabel backgroundlabel;
	public ManagerSuspendSub(ManagerMainUI managerMainUI) 
	{
		super();
		this.managerMainUI=managerMainUI;
		System.out.println("ManagerSuspendSub created.");
		this.muip=new ManagerUIP();
		initialize();
	}
	private void initialize()
	{
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		setLayout(null);
    	separator.setBounds(0, 57, 855, 30);
		add(separator);
		lblOblManager = new JLabel("OBL Manager-Suspend Subscribers");
		lblOblManager.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblOblManager.setBounds(173, 0, 368, 58);
		add(lblOblManager);
		btnExit = new JButton("Exit");
		btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnExit.setBounds(707, 11, 83, 41);
		add(btnExit);
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnBack.setBounds(10, 11, 83, 41);
		add(btnBack);
		btnLogout = new JButton("Logout");
		btnLogout.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnLogout.setBounds(614, 11, 83, 41);
		add(btnLogout);
		
		searchBySubID = new JTextField();
		searchBySubID.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		searchBySubID.setBounds(227, 70, 265, 24);
		searchBySubID.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(searchBySubID);
		searchBySubID.setColumns(10);
		
		searchBySubFName = new JTextField();
		searchBySubFName.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		searchBySubFName.setBounds(227, 123, 265, 24);
		searchBySubFName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(searchBySubFName);
		searchBySubFName.setColumns(10);
		
		searchBySubLName = new JTextField();
		searchBySubLName.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		searchBySubLName.setBounds(227, 175, 265, 24);
		searchBySubLName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(searchBySubLName);
		searchBySubLName.setColumns(10);
		
		lblBySIDLabel = new JLabel("Search Subscribers by ID :");
		lblBySIDLabel.setBounds(20, 70, 197, 24);
		lblBySIDLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblBySIDLabel);
		
		lblByFNameLabel = new JLabel("Search Subscribers by Fname :");
		lblByFNameLabel.setBounds(20, 123, 197, 24);
		lblByFNameLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblByFNameLabel);
		
		lblByLNameLabel = new JLabel("Search Subscribers by Lname :");
		lblByLNameLabel.setBounds(20, 174, 197, 24);
		lblByLNameLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblByLNameLabel);
		
		btnClear = new JButton("Clear");
		btnClear.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnClear.setBounds(56, 234, 97, 41);
		add(btnClear);
		
		btnSearch = new JButton("Search");
		btnSearch.setBounds(395, 234, 97, 41);
		btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(btnSearch);
		
		rdbtnSearch = new JRadioButton("Search ONLY");
		rdbtnSearch.setOpaque(false);
		rdbtnSearch.setSelected(true);
		rdbtnSearch.setBounds(260, 289, 109, 23);
		add(rdbtnSearch);
		
		rdbtnShowAll = new JRadioButton("Show All Unsuspeded");
		rdbtnShowAll.setSelected(false);
		rdbtnShowAll.setBounds(655, 289, 139, 23);
		add(rdbtnShowAll);
		
		lblSearchForUnsuspended = new JLabel("Search for Unsuspended Subscribers :");
		lblSearchForUnsuspended.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSearchForUnsuspended.setBounds(10, 286, 240, 24);
		add(lblSearchForUnsuspended);
		
		lblShowAllUnsuspended = new JLabel("Show All Unsuspended Subscribers :");
		lblShowAllUnsuspended.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblShowAllUnsuspended.setBounds(428, 286, 232, 24);
		add(lblShowAllUnsuspended);
		
		Object ColNames[] = { "ID", "Password", "Permmision","Status" ,"FirstName","LastName","Address","E-mail","PhoneNum" };
		SubTable = new SimpleTable(null, ColNames, 10, 328, 780, 198);
	    add(SubTable.tblScrollPane,BorderLayout.CENTER);
	    
	    btnSuspend = new JButton("Suspend");
	    btnSuspend.setFont(new Font("Tahoma", Font.PLAIN, 15));
	    btnSuspend.setBounds(395, 528, 97, 41);
	    btnSuspend.setEnabled(false);
	    add(btnSuspend);
	    
	    lblPresentLateSubscribers = new JLabel("Present Late Subscribers :");
	    lblPresentLateSubscribers.setFont(new Font("Tahoma", Font.PLAIN, 14));
	    lblPresentLateSubscribers.setBounds(18, 528, 232, 24);
	    add(lblPresentLateSubscribers);
	    
	    rdbtnShowLateSubscribers = new JRadioButton("Show Late Subscribers");
	    rdbtnShowLateSubscribers.setSelected(false);
	    rdbtnShowLateSubscribers.setBounds(186, 528, 139, 23);
	    rdbtnShowLateSubscribers.setOpaque(false);
	    add(rdbtnShowLateSubscribers);
	    
	    backgroundlabel = new JLabel("");
	    backgroundlabel.setIcon(OBLSettings.IMG_MANAGERSuspendSubscriber);
	    backgroundlabel.setBounds(0, 57, 800, 543);
	    add(backgroundlabel);
		
//================================================================		
//      Action Listeners
//================================================================
	    SubTable.table.getSelectionModel().addListSelectionListener(new ListSelectionListener() 
		{
			public void valueChanged(ListSelectionEvent e) 
			{
				JTable table = SubTable.table;

				if(table.getRowCount() > 0)	
				{	
					int row = SubTable.table.getSelectedRow();
					
					btnSuspend.setEnabled(true);
					if(row>=0)
					{ 
						
			    		Object[] rowData = new Object[table.getColumnCount()];  
			    		for (int i = 0; i < table.getColumnCount(); i++) 
			    		{  
					         rowData[i] = table.getValueAt(row, i);  
			    		} 
			    		status_user=Integer.parseInt(rowData[3].toString());
			    		id_user=(rowData[0].toString());
			    		permission_user=Integer.parseInt(rowData[2].toString());
			    		rowToDelete=row;
			    		
			    		
			    	
					}
				}
			}
			
		});  
//================================================================	    
	    btnSuspend.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				
				if(!(((status_user==0) || (status_user==1))&& (permission_user!=5)))
					JOptionPane.showMessageDialog( WelcomeUI.wui,"Can't Suspend User","",JOptionPane.INFORMATION_MESSAGE);
					else
					{	
						int result=muip.suspendSubscriber(id_user);
						
						     if( result == 1)
						     {		
						    	 JOptionPane.showMessageDialog( WelcomeUI.wui,"User with ID=" +
											id_user+"Has Been Suspended!!!","",JOptionPane.INFORMATION_MESSAGE);
						    	 SubTable.model.removeRow(rowToDelete);
						    	 SubTable.table.clearSelection();
						     }
						     else
							   JOptionPane.showMessageDialog( WelcomeUI.wui,"Suspension was Unsuccessful!","",JOptionPane.WARNING_MESSAGE);
					}
			}
		});
			    
	    
	    
//================================================================
btnClear.addActionListener(new ActionListener()
{
	public void actionPerformed(ActionEvent arg0)
	{
		 searchBySubID.setText("");
		 searchBySubFName.setText("");
		 searchBySubLName.setText("");
		 btnSuspend.setEnabled(false);
		 SubTable.model.setRowCount(0);	 
	}
});
//================================================================	
rdbtnShowLateSubscribers.addActionListener(new ActionListener()
{
	public void actionPerformed(ActionEvent arg0)
	{
		rdbtnShowLateSubscribers.setSelected(true);
		rdbtnShowAll.setSelected(false);
		rdbtnSearch.setSelected(false);
		searchBySubID.setText("");
		searchBySubFName.setText("");
		searchBySubLName.setText("");
		btnSearch.setEnabled(false);
		searchBySubID.setEnabled(false);
		searchBySubFName.setEnabled(false);
		searchBySubLName.setEnabled(false);
		SubTable.model.setRowCount(0);
		UsersMSG lateSubsMsg=muip.getLateSubscribers();
		for (User var : lateSubsMsg.getUsers().values())
        {
			SubTable.model.addRow(new Object[] {var.getID(),var.getPassword(),var.getPermmision(),var.getStatus(),var.getFname(),var.getLname()
					,var.getAddress(),var.getEmail(),var.getPhone()});
        }
		btnSuspend.setEnabled(false);
	}
});

//================================================================	
btnBack.addActionListener(new ActionListener() 
{
	public void actionPerformed(ActionEvent e) 
	{
		 searchBySubID.setEnabled(true);
		 searchBySubFName.setEnabled(true);
		 searchBySubLName.setEnabled(true);
		 searchBySubID.setText("");
		 searchBySubFName.setText("");
		 searchBySubLName.setText("");
		 btnSuspend.setEnabled(false);
		 SubTable.model.setRowCount(0);	 
		 rdbtnSearch.setSelected(true);
		 btnSearch.setEnabled(true);
		 rdbtnShowAll.setSelected(false);
		 btnSuspend.setEnabled(false);
		 rdbtnShowLateSubscribers.setSelected(false);
         WelcomeUI.wui.switchPanel("OBL Manager",managerMainUI ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
	}
});	
rdbtnShowAll.addActionListener(new ActionListener() 
{
	public void actionPerformed(ActionEvent e) 
	{
		rdbtnShowAll.setSelected(true);
		rdbtnShowLateSubscribers.setSelected(false);
		rdbtnSearch.setSelected(false);
		btnSearch.setEnabled(false);
		searchBySubID.setEnabled(false);
		searchBySubFName.setEnabled(false);
		searchBySubLName.setEnabled(false);
		SubTable.model.setRowCount(0);	 
		UsersMSG UnsuspendedSubsMsg=muip.getAllUnsuspendedSubs();
		for (User var : UnsuspendedSubsMsg.getUsers().values())
        {
			SubTable.model.addRow(new Object[] {var.getID(),var.getPassword(),var.getPermmision(),var.getStatus(),var.getFname(),var.getLname()
					,var.getAddress(),var.getEmail(),var.getPhone()});
        }
		btnSuspend.setEnabled(false);
	}
});	
rdbtnSearch.addActionListener(new ActionListener() 
{
	public void actionPerformed(ActionEvent e) 
	{
		 rdbtnSearch.setSelected(true);
		 rdbtnShowLateSubscribers.setSelected(false);
		 rdbtnShowAll.setSelected(false);
		 btnSearch.setEnabled(true);
		 searchBySubID.setEnabled(true);
		 searchBySubFName.setEnabled(true);
		 searchBySubLName.setEnabled(true);
		 SubTable.model.setRowCount(0);
		 btnSuspend.setEnabled(false);
	}
});	
 btnSearch.addActionListener(new ActionListener()  
 {
	public void actionPerformed(ActionEvent e) 
	{
		SubTable.model.setRowCount(0);
		ManagerSerachSubMSG managerSerachSubMSG = muip.searchUnsuspendedSubs(searchBySubID.getText(),searchBySubFName.getText(),searchBySubLName.getText());
		if(managerSerachSubMSG!=null)
		{
		 for (User var : managerSerachSubMSG.getUsers().values())
         {
			SubTable.model.addRow(new Object[] {var.getID(),var.getPassword(),var.getPermmision(),var.getStatus(),var.getFname(),var.getLname()
					,var.getAddress(),var.getEmail(),var.getPhone()});
         }
		}
		else
			System.out.println("addActionListener searchUnsuspendedSubs ");
		btnSuspend.setEnabled(false); 
	}
	
 });	
 }
	
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
}
